extends Node
class_name StateMachine
var states : Dictionary = {}
@export var current_state : State
func _ready() -> void:
	assert(current_state is State, "FiniteStateMachine: 状态机没有初始节点")
	if get_child_count() > 0:
		get_state()
	current_state.enter()

## 从子节点中获取状态脚本
func get_state(node: Node = self) -> void:
	for child in node.get_children(true):
		if child is State:
			states[child.name] = child
			(child as State).ready()
			child.stateMachine = self
		elif child.get_child_count() > 0:
			get_state(child)

func _process(delta: float) -> void:
	current_state.process(delta)
	pass
func _physics_process(delta: float) -> void:
	current_state.physics_process(delta)
	pass

## 改变状态
func change_to_state(new_state: String) -> void:
	if new_state in states:
		current_state.exit()
		print_debug("退出" + current_state.to_string())
		current_state = states[new_state]
		current_state.enter()
		print_debug("进入" + current_state.to_string())
	else :
		print_debug("状态转换出错，请检查状态填写是否正确")
